package furns.web;


import com.google.gson.Gson;
import furns.entity.Cart;
import furns.entity.CartItem;
import furns.entity.Furn;
import furns.service.FurnService;
import furns.service.impl.FurnServiceImpl;
import furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class CartServlet extends BasicServlet{
    private FurnService furnService = new FurnServiceImpl();
    //添加家居到购物车
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过id获取家居信息
        int id = DataUtils.strPareInt(req.getParameter("id"), 0);
        Furn furn = furnService.queryFurnById(id);
        //异常判断
        //todo

        CartItem cartItem = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1);

        //创建session，并判断是否存在该属性
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        //存在就直接加入
        cart.addItem(cartItem);
        System.out.println(cart);

        //添加完毕后，需要返回到添加家居的页面
        //获取请求头中的信息
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

    //更新购物车
    protected void updateItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.strPareInt(req.getParameter("id"), 0);
        int count = DataUtils.strPareInt(req.getParameter("count"), 1);
        Cart cart = (Cart)req.getSession().getAttribute("cart");
        if (cart !=null){
            cart.updateItem(id,count);
        }
        //回到请求更新购物车的页面
        resp.sendRedirect(req.getHeader("Referer"));
    }

    //删除购物车
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.strPareInt(req.getParameter("id"), 0);
        Cart cart = (Cart)req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.delItem(id);
        }
        //回到请求更新购物车的页面
        resp.sendRedirect(req.getHeader("Referer"));
    }
    //清空购物车
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart)req.getSession().getAttribute("cart");
        if (cart != null) {
          cart.clearItem();
        }
        //回到请求更新购物车的页面
        resp.sendRedirect(req.getHeader("Referer"));
    }

    //添加家居到购物车(Ajax)
    protected void addItemByAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过id获取家居信息
        int id = DataUtils.strPareInt(req.getParameter("id"), 0);
        Furn furn = furnService.queryFurnById(id);
        CartItem cartItem = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1);

        //创建session，并判断是否存在该属性
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        //存在就直接加入
        cart.addItem(cartItem);

        //添加完毕后，使用ajax局部刷新
        //将数据返回给前端,进行局部刷新
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("cartTotalCount",cart.getTotalCount());
        //转成json格式
        Gson gson = new Gson();
        String regStr = gson.toJson(resultMap);
        //返回
        resp.getWriter().write(regStr);
    }
}
